from lxml import etree
from cvirt.cnode.libvirtXmlObject.baseDomain import XmlObject

class cputune(XmlObject):
    ROOT_name = 'cputune'
    def __init__(self,parse_xml=None):
        #[{vcpu:0,cpuset:1-4},{}]
        self.vcpupin = []
        self.shares = None
        self.period = None
        self.quota = None
        super(cputune,self).__init__(parse_xml=parse_xml)

    def _parse_xml(self, xmldoc):
        for vcpupin in xmldoc.findall('./vcpupin'):
            self.vcpupin.append(dict(vcpupin=vcpupin.get('vcpu'),cpuset=vcpupin.get('cpuset')))

        shares = xmldoc.find('./shares')
        if shares is not None:
            self.shares = shares[0].text

        period = xmldoc.find('./period')
        if period is not None:
            self.period = period[0].text

        quota = xmldoc.find('./quota')
        if quota is not None:
            self.quota = quota[0].text


    def _get_xml_config(self, root):
        for v in self.vcpupin:
            pin = etree.Element('vcpupin',vcpu=v['vcpu'],cpuset=v['cpuset'])
            root.append(pin)
        if self.shares:
            shares = etree.Element('shares')
            shares.text = self.shares
            root.append(shares)

        if self.period:
            period = etree.Element('period')
            period.text = self.period
            root.append(period)

        if self.quota:
            quota = etree.Element('quota')
            quota.text = self.quota
            root.append(quota)

        return root

class memtune(XmlObject):
    ROOT_name = 'memtune'
    def __init__(self,parse_xml=None):
        #{unit:'G',size:'5'}
        self.hard_limit = None
        self.soft_limit = None
        self.swap_hard_limit = None
        self.min_guarantee = None
        super(memtune,self).__init__(parse_xml=parse_xml)

    def _parse_xml(self, xmldoc):
        pass

    def _get_xml_config(self, root):
        pass


